package com.liangzhong.modules.activity.mediator
{
	import com.common.queueloader.QueueLoader;
	import com.common.queueloader.QueueLoaderEvent;
	import com.common.queueloader.QueueVo;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.events.ActivityEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.mediator.ModuleMediator;
	import com.liangzhong.proxyconstant.activity.ActivityConst;
	import com.lz.football.module.activity.IActivity;
	import com.lz.football.module.activity.IVipWard;
	import com.lz.football.module.base.IComponent;
	import com.lz.football.module.base.IModuleView;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	
	import lz.controls.managers.AnimationManager;
	import lz.controls.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.INotification;
	
	public class VipMediator extends ModuleMediator
	{
		public static const MediatorName:String= "VipMediator";
		public function VipMediator(mediatorName:String=null, viewComponent:Object=null, param:Object=null)
		{
			super(mediatorName, viewComponent, param);
		}
		
		
		/**
		 *获取 实例 
		 * @return 
		 * 
		 */		
		public function get clubView():DisplayObjectContainer
		{
			return viewComponent as DisplayObjectContainer;
		}
		/**
		 *添加事件 
		 * 
		 */		
		override public function addEventListeners():void
		{
			(clubView).addEventListener(ActivityEvent.ACTIVITY_VIP_DAILY_AWARD,onAwardHandler);
			(clubView).addEventListener(ModuleEvent.REMOVE_MODULE,onRemoveModule);
		}
		
		protected function onRemoveModule(event:ActivityEvent):void
		{
			removeMediator();
		}
		
		protected function onAwardHandler(event:ActivityEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(ActivityConst.VIP_DAILY_AWARD,event.param);
		}
		/**
		 * 删除事件 
		 * 
		 */
		override public function removeEventListeners():void
		{
			(clubView).removeEventListener(ActivityEvent.ACTIVITY_VIP_DAILY_AWARD,onAwardHandler);
			(clubView).removeEventListener(ModuleEvent.REMOVE_MODULE,onRemoveModule);
		}
		/**
		 * 监听信息
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [ActivityConst.GET_VIP_INFO,
				ActivityConst.GET_VIP_DAILY_AWARD];
		}
		/**
		 * 处理信息
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case ActivityConst.GET_VIP_DAILY_AWARD:
					(viewComponent as IActivity).award(notification.getBody());
					break;
				case ActivityConst.GET_VIP_INFO:
//					showUI();
					(viewComponent as IActivity).baseinformation(notification.getBody());
					break;
				
			}
		}
		/**
		 * 
		 * 注册
		 */		
		override public function onRegister():void
		{
			
			resName = ResourceConst.VIP_SWF;
			if(ResourceManager.instance.isContainResource(ResourceConst.VIP_SWF))
			{
								showUI();
//				this.sendNotification(ActivityConst.LEVEL_INFO);
			}
			else
			{
				loader = new QueueLoader();
				var clubConfig :XML = XML(ResourceManager.instance.getResource(ResourceConst.MODULE_RES_CONFIG)).vip[0];
				bytesTotal = int(clubConfig.@totalBytes);
				var releaseVersion :String;
				for each(var item :XML in clubConfig.item)
				{
					releaseVersion = "?" + item.@version + "." + item.@itemType;
					loader.addQueue(new QueueVo(item.@resName,item.@id,item.@id),item.@url + releaseVersion);
				}
				loadRes();
			}
		}
		/**
		 *加载资源完毕 
		 * @param e
		 * 
		 */		
		override protected function onLoadComplete(e:QueueLoaderEvent):void
		{
									showUI();
			
			super.onLoadComplete(e);
			ResourceManager.instance.putResource(ResourceConst.VIP_SWF,resList);
//			this.sendNotification(ActivityConst.LEVEL_INFO);
		}
		private function showUI():void
		{
			var cView :IModuleView = Reflection.createInstance("com.lz.football.module.activity.vip.VipView");
			cView.initUI();
			viewComponent = cView;
			
			GlobalDef.GameWindowContainer.addChild(viewComponent as DisplayObject);
			addEventListeners();
			
			//测试vip奖励动画 
//			AnimationManager.addItem({func:vipDailyWard,data:{lv:2}});
//			AnimationManager.addItem({func:vipLvWard,data:{lv:2}});

		}
//		
//		
//		/**
//		 *vip每日奖励动画  
//		 * 
//		 */
//		private function vipDailyWard($obj:Object):void
//		{
//			var wardView:IComponent = Reflection.createInstance("com.lz.football.module.activity.vip.VipDailyWardView");
//			wardView.initUI();
//			(wardView as IVipWard).showWard($obj);
//			GlobalDef.GameWindowContainer.addChild(wardView as DisplayObject);
//		}
		/**
		 *vip等级奖励动画  
		 * 
		 */
		private function vipLvWard($obj:Object):void
		{
			var wardView:IComponent = Reflection.createInstance("com.lz.football.module.activity.vip.VipLvWardView");
			wardView.initUI();
			(wardView as IVipWard).showWard($obj);
			PopUpManager.addWindow(wardView as DisplayObject,GlobalDef.AlertContainer,0,0,false);
		}
		/**
		 * 删除 mediator
		 * 
		 */		
		override public function removeMediator():void
		{
			facade.removeMediator(MediatorName);
		}
		/**
		 *删除 清理 
		 * 
		 */		
		override public function onRemove():void
		{
			super.onRemove();
			removeEventListeners();
			(viewComponent as IModuleView).destroyView();
			viewComponent = null;
		}
	}
}